package com.andnux.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.andnux.android.MainApp;
import com.andnux.android.data.Orders;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.data.Works;
import com.andnux.android.databinding.ActivityBuyBinding;
import com.andnux.android.network.DesignCloudApi;
import com.andnux.android.network.GsonUtils;
import com.andnux.android.network.Result;
import com.andnux.android.network.RetrofitClient;
import com.andnux.android.network.SuccessConsumer;
import com.andnux.android.utils.DateUtils;
import com.andnux.android.utils.ImageLoader;
import com.andnux.android.utils.StringUtils;
import com.google.gson.reflect.TypeToken;

import java.util.Date;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class BuyActivity extends BaseActivity<ActivityBuyBinding> {

    private Works works;
    private DesignCloudApi api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = RetrofitClient.getInstance().getServerApi();
        works = (Works) getIntent().getSerializableExtra("works");
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ImageLoader.load(works.getUsers().getAvatar(), binding.hedImage);
        Date updateTime = works.getUsers().getUpdateTime();
        if (updateTime == null) {
            updateTime = works.getCreateTime();
        }
        binding.title.setText(works.getUsers().getNickname());
        binding.time.setText(DateUtils.format(updateTime, DateUtils.DATE_PATTERN));
        binding.worksPrice.setText(StringUtils.toString(works.getPrice()));
        binding.allPrice1.setText(StringUtils.toString(works.getPrice()));
        binding.allPrice2.setText(StringUtils.toString(works.getPrice()));
        binding.worksTitle.setText(StringUtils.toString(works.getTitle()));
        String json = works.getFiles();
        List<String> files = GsonUtils.gson.fromJson(json, new TypeToken<List<String>>() {
        }.getType());
        ImageLoader.load(files.get(0), binding.worksImage);

        binding.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doBuy();
            }
        });
    }

    private void doBuy() {
        SystemUsers users = MainApp.getApp().getSystemUsers();
        Orders vo = new Orders();
        vo.setBuyUserId(String.valueOf(users.getId()));
        vo.setSellUserId(works.getUserId());
        vo.setStatus(0);
        vo.setTitle(works.getTitle());
        vo.setWorksId(String.valueOf(works.getId()));
        vo.setPrice(String.valueOf(works.getPrice()));
        vo.setContent(works.getContent());
        vo.setDeleted("0");
        vo.setFiles(works.getFiles());
        vo.setRemark("");
        addDisposable(api.orderInsert(vo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SuccessConsumer<Result<Orders>>() {
                    @Override
                    public void onSuccess(Result<Orders> result) {
                        Toast.makeText(BuyActivity.this, "下单成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BuyActivity.this, MyOrderActivity.class);
                        intent.putExtra("status", 0);
                        startActivity(intent);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        if (throwable != null) {
                            Toast.makeText(BuyActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }));
    }
}